﻿using System;
using System.Text;
using System.Windows.Forms;
using WFNetLib;

namespace 使用次数设定
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public byte[] strToHexByte(string hexString, string spilt)
        {
            if (spilt != "")
                hexString = hexString.Replace(spilt, "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        public string byteToHexStr(byte[] bytes, int offset, int count, string spilt)
        {
            StringBuilder strB = new StringBuilder();
            if (bytes != null)
            {
                for (int i = 0; i < count; i++)
                {
                    strB.Append(bytes[offset + i].ToString("X2"));
                    strB.Append(spilt);
                }
            }
            return strB.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] keys;
            try
            {
                keys=strToHexByte(maskedTextBox1.Text, " ");
            }
            catch { MessageBox.Show("输入的机器码有问题，请检查!!"); return; }
            if (keys.Length != 8)
            {
                MessageBox.Show("输入的机器码有问题，请检查!!"); 
                return;
            }
            lcyHashCal lcy = new lcyHashCal();
            byte[] Encode = lcy.HashCal(keys, (int)(numericUpDown1.Value+1));
            byte[] s = new byte[9];
            byte t = (byte)(numericUpDown1.Value + 1);
            t = (byte)(t ^ 'w');
            t = (byte)(t ^ 'f');
            for (int i = 0; i < 4; i++)
                s[i] = Encode[i];
            s[4] = t;
            for (int i = 0; i < 4; i++)
                s[i + 5] = Encode[i + 4];
            textBox3.Text = byteToHexStr(s, 0, 9, " ");
            MessageBox.Show("生成完成");
        }
    }
}
